<template>
  <div>
    <div class="page-body" v-loading="loading">
      <div class="page-body-content">
        <div class="page-main">
          <div class="page-content">
            <div class="page-logo">
              <img :src="LOGIN_IMG" />
            </div>
            <!-- <div class="page-title">客户数据交换平台</div> -->
            <el-form ref="formRef" label-position="top" :model="loginForm" label-width="0px"
              @keyup.enter.native="onSubmit">
              <el-form-item label="用户名">
                <el-input v-model="loginForm.username" placeholder="请输入用户名" ref="input"></el-input>
              </el-form-item>
              <el-form-item label="密码">
                <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" show-password></el-input>
              </el-form-item>
              <el-button type="primary" style="width: 100%;margin-top:20px" @click="onSubmit">登录</el-button>
            </el-form>
          </div>
        </div>
      </div>
      <div class="page-body-bg" :style="{
        'background-image': `url(${bg})`
      }">
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, nextTick } from "vue";
import { ElMessage } from "element-plus";
export default defineComponent({
  name: "Login",
})
</script>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { appAction } from "@/store/app";
import router from "@/utils/router";
import { CAJ } from "@/utils/bus";
import { request } from "@/utils/axios";
import { appState } from "@/store/app";
import { LOGIN_IMG } from "@/config";

let input = ref()
let bg = ref<string>(require("@/assets/view/Login/bg.jpg"));

let loginForm = reactive({
  username: "",
  password: ""
})

/**
 * 加载loading
 */
let loading = ref<boolean>(false);
/**
 * 用户身份列表
 */
let TicketList = ref<{
  GrantType: string
  Id: number
  Name: string
  Ticket: string
}[]>([])
/**
 * 选择用户身份
 */
let Ticket = ref<string>("");

let visible = ref(false)
/**
 * 点击登录的时候
 */
function onSubmit() {
  if (loginForm.username && loginForm.password) {
    let data = {
      r: loginForm.username,//账号/手机号",
      z: loginForm.password, //密码/验证码",
    }
    loading.value = true;
    request("/m/sys/Login/Login", CAJ(data), {
      contentType: "form"
    })
      .then(async (res) => {
        appAction.setToken(res.Token);
        appAction.setGrantType(res.GrantType);
        router.push({
          name: "AppHome"
        })
        // loading.value = false;
      })
      .catch(() => {
        loading.value = false;
      })
  } else {
    ElMessage.error("请输入账号和密码！")
  }
}

/**
 * 根据账户信息 获取 ticket
 */
function getTicket(data: any): Promise<any[]> {
  return new Promise((resolve, reject) => {
    request("/Login/GetPcLoginTicket", CAJ(data), {
      contentType: "form"
    })
      .then((res) => {
        resolve(res)
      })
      .catch(reject)
  })

}

/**
 * 页面加载
 */
if (appState.token) {
  router.push({
    name: "AppHome"
  })
}
nextTick(() => {
  input.value.focus()
})
</script>

<style lang="less" scoped>
.page-body {
  width: 100vw;
  height: 100vh;
  background-size: cover;
  background-position: center;
}

.page-body-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: center;
}

.page-body-content {
  position: relative;
  width: 100%;
  height: 100%;
  z-index: 2;
  display: flex;
}

.page-body-left {
  flex-grow: 1;
  width: 0;
  overflow: hidden;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

}

.page-logo {
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 3px;
  padding: 20px 0;
  // box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.1);
  // backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.5);
  margin-bottom: 30px;

  >img {
    width: 80%;
    display: block;
  }
}

.page-main {
  width: 400px;
  height: calc(100% - 100px);
  position: absolute;
  right: 50px;
  top: 50px;
  flex-shrink: 0;
  backdrop-filter: blur(10px);
  background-color: rgba(0, 0, 0, 0.3);
  // background-color: black;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;

  padding-bottom: 10vh;
}

.page-content {
  width: 300px;
}


.page-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
  text-align: center;
}






// .page-main {
//   height: 380px;
//   display: flex;
//   border-radius: 3px;
//   overflow: hidden;
//   box-shadow: 0 0 10px 1px rgba(0, 0, 0, 0.1);
//   backdrop-filter: blur(10px);
// }

// .page-main-left {
//   // background-color: var(--primaryColor);
//   // background-color: rgba(255, 255, 255, 0.7);
//   background-color: white;
//   display: flex;
//   justify-content: center;
//   align-items: center;
//   padding: 0 30px;

//   >img {
//     width: 220px;
//     display: block;
//   }
// }

// .page-main-right {
//   backdrop-filter: blur(10px);
//   // background-color: var(--primaryColor);
//   background-color: rgba(255, 255, 255, 0.3);
//   padding: 0 30px;
//   flex-shrink: 0;
//   height: 100%;
//   display: flex;
//   justify-content: center;
//   align-items: center;
// }


// .page-main-content {
//   width: 250px;
//   margin: 0 auto;
// }
</style>
